package com.wind.taobao.api.trade.item.convert
{
	import com.wind.taobao.api.trade.item.model.Postage;
	import com.wind.taobao.core.util.TaobaoConstants;
	
	import mx.collections.ArrayCollection;


	/**
	 * Postage转换器
	 * @author fxy
	 *
	 */
	public class PostageConvert
	{
		/**
		 * 转换Postage集合
		 */
		public static function convertPostages(value:Object, type:String="json"):ArrayCollection
		{
			var postages:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							postages.addItem(convertPostage((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return postages;
		}

		/**
		 * 转换Postage
		 */
		public static function convertPostage(value:Object, type:String="json"):Postage
		{
			var postage:Postage=new Postage();
			if (value == null)
				return postage;
			if (type == TaobaoConstants.XML)
			{
				postage=convertXMLToPostage(XML(value), postage);
			}
			else
			{
				postage=convertJSONToPostage(value, postage);
			}
			return postage;
		}

		/**
		 * JSON转换为Postage
		 */
		private static function convertJSONToPostage(json:Object, postage:Postage):Postage
		{
			if (json.hasOwnProperty(TaobaoConstants.POSTAGE_ID))
			{
				postage.postage_id=json[TaobaoConstants.POSTAGE_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.NAME))
			{
				postage.name=json[TaobaoConstants.NAME];
			}
			if (json.hasOwnProperty(TaobaoConstants.MEMO))
			{
				postage.memo=json[TaobaoConstants.MEMO];
			}
			if (json.hasOwnProperty(TaobaoConstants.CREATED))
			{
				postage.created=json[TaobaoConstants.CREATED];
			}
			if (json.hasOwnProperty(TaobaoConstants.MODIFIED))
			{
				postage.modified=json[TaobaoConstants.MODIFIED];
			}
			if (json.hasOwnProperty(TaobaoConstants.POST_PRICE))
			{
				postage.post_price=json[TaobaoConstants.POST_PRICE];
			}
			if (json.hasOwnProperty(TaobaoConstants.POST_INCREASE))
			{
				postage.post_increase=json[TaobaoConstants.POST_INCREASE];
			}
			if (json.hasOwnProperty(TaobaoConstants.EXPRESS_PRICE))
			{
				postage.express_price=json[TaobaoConstants.EXPRESS_PRICE];
			}
			if (json.hasOwnProperty(TaobaoConstants.EXPRESS_INCREASE))
			{
				postage.express_increase=json[TaobaoConstants.EXPRESS_INCREASE];
			}
			if (json.hasOwnProperty(TaobaoConstants.EMS_PRICE))
			{
				postage.ems_price=json[TaobaoConstants.EMS_PRICE];
			}
			if (json.hasOwnProperty(TaobaoConstants.EMS_INCREASE))
			{
				postage.ems_increase=json[TaobaoConstants.EMS_INCREASE];
			}
			if (json.hasOwnProperty(TaobaoConstants.POSTAGE_MODE))
			{
				postage.postage_mode_list=PostageModeConvert.convertPostageMode(json[TaobaoConstants.POSTAGE_MODE]);
			}
			return postage;
		}

		/**
		 * XML转换为Postage
		 */
		private static function convertXMLToPostage(value:XML, postage:Postage):Postage
		{
			return postage;
		}

	}
}